<template>
  <div class="cards-container">
    <div class="cards-grid">
      <div
          v-for="item in itemsList"
          :key="item.itemId"
          class="inspection-card"
      >
        <div class="card-header">
          <h3>{{ item.itemName }}</h3>
          <el-tag :type="getItemTypeTag(item.itemType)" size="small">
            {{ item.itemType || '未分类' }}
          </el-tag>
        </div>

        <div class="card-content">
          <div class="card-row">
            <span class="label">检验方法:</span>
            <span>{{ item.inspectionMethod }}</span>
          </div>

          <div class="card-row">
            <span class="label">标准范围:</span>
            <span>{{ item.minLimit }} ~ {{ item.maxLimit }}</span>
          </div>

          <div class="card-row">
            <span class="label">标准值:</span>
            <span>{{ item.standardValue }}</span>
          </div>
        </div>

        <div class="card-footer">
          <span class="update-time">更新: {{ formatTime(item.updatedAt) }}</span>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { listItems } from "@/api/quality-service/items"

const itemsList = ref([])

// 获取数据
const fetchItems = async () => {
  try {
    const response = await listItems({
      pageNum: 1,
      pageSize: 100 // 获取足够多的数据
    })
    itemsList.value = response.rows
  } catch (error) {
    console.error('获取检验项目失败:', error)
  }
}

// 项目类型标签样式
const getItemTypeTag = (type) => {
  const typeMap = {
    '外观': 'success',
    '尺寸': 'warning',
    '性能': 'danger',
    '安全': 'info'
  }
  return typeMap[type] || ''
}

// 格式化时间
const formatTime = (time) => {
  if (!time) return '未知'
  return new Date(time).toLocaleDateString()
}

// 初始化加载数据
onMounted(() => {
  fetchItems()
})
</script>

<style scoped>
.cards-container {
  padding: 20px;
}

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.inspection-card {
  border-radius: 8px;
  border: 1px solid #ebeef5;
  background-color: #fff;
  padding: 16px;
  transition: all 0.3s;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.inspection-card:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid #f0f0f0;
}

.card-header h3 {
  margin: 0;
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.card-content {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.card-row {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
}

.label {
  color: #666;
  margin-right: 8px;
}

.card-footer {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid #f0f0f0;
  font-size: 12px;
  color: #999;
  text-align: right;
}
</style>